const app = getApp();
var util = require('../../../utils/util.js');
var api = require('../../../config/api.js');
var user = require('../../../services/user.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    articleList:[],
    current_page:1,
    last_page:1,
    total:0,
    per_page:15,
    category_id: null,
    tabNav:null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function () {    
    this.getArticleListData()
  },
  getArticleListData: function () {
    let that = this;
    
    util.request(api.ArticleList, { category_id: this.data.category_id, page: this.data.current_page}).then(res => {
      if (res.code === 200) {
        var oldarticleList = that.data.articleList
        var newarticleList = oldarticleList.concat(res.data.articleList.data)
        that.setData({
          articleList: newarticleList,
          current_page: res.data.articleList.current_page,
          last_page: res.data.articleList.last_page,
          per_page: res.data.articleList.per_page,
          total: res.data.articleList.total,
          tabNav: res.data.tabNav
        });
       
      }
      wx.hideLoading();
    }).catch(() => {
      // wx.navigateTo({
      //   url: '/pages/auth/login/login',
      // })
      wx.hideLoading();
    });
  },
  tabChange: function(e){
    this.setData({
      articleList: [],
      category_id: e.detail,
      current_page: 1
    })
    this.getArticleListData()
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    // this.setData({ current_page: parseInt(this.data.current_page)+1})
    // this.getArticleListData();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this
    wx.showLoading({
      title: '加载中！',
      success:function(){
        that.setData({ current_page: parseInt(that.data.current_page) + 1 })
        that.getArticleListData();
      }
    })
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})